﻿using IRepositoryBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositoryEntity;
using RepositoryEntity.Extension;
using RepositoryEntity.ResultModel.MenuInfoResultModel;
using RepositorySystem.Models;
using RepositorySystem.Models.MenuInfoViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositoryAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MenuInfoController : ControllerBase
    {
        private IMenuInfoBll _menuInfoBll;
        public MenuInfoController(IMenuInfoBll menuInfoBll)
        {
            _menuInfoBll = menuInfoBll;
        }


        /// <summary>
        /// 菜单集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInitMenus()
        {
            var res = new GetInitMenusResultModel();
            
            UserInfo userInfo = this.GetSessionUserInfo();
            JsonResult result;
            result = new JsonResult(res);
            if (userInfo != null)
            {
                //获取菜单集合信息
                List<MenuInfoResultModel> menus = _menuInfoBll.GetInitMenus(userInfo);

                res.SetMenuData(menus);
            }

            return result;

        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenuInfo(AddMenuInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            if (model.Title == null)
            {
                res.Msg = "菜单标题不能为空";
                return result;
            }
            else if (_menuInfoBll.IsHadMenuInfo(model.Title))
            {
                res.Msg = "当前菜单标题已存在";
                return result;
            }
            else
            {
                MenuInfo menuInfo = new MenuInfo()
                {
                    ID = Guid.NewGuid().ToString(),
                    Title = model.Title,
                    Description = model.Description,
                    Level = model.Level,

                    Sort = model.Sort,
                    Href = model.Href,
                    ParentId = model.ParentId,
                    Icon = model.Icon,
                    Target = model.Target,
                    CreateTime = DateTime.Now,
                    IsDelete = false

                };
                bool isSuccess = _menuInfoBll.AddMenuInfo(menuInfo);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Msg = "添加成功";
                    return result;

                }
                else
                {
                    return result;
                }

            }

        }

        /// <summary>
        /// 获取菜单集合/分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetMenuInfoList(int page, int limit, string title)
        {

            int count;
            object menuInfo = _menuInfoBll.GetMenuInfoListByPage(page, limit, out count, title);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = menuInfo
            });
        }

        /// <summary>
        /// 获取更新修改页面
        /// </summary>
        /// <param name="menuInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetUpdateMenuInfo(string menuInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            //获取菜单信息
            MenuInfo menuInfo = _menuInfoBll.GetMenuInfo(menuInfoId);
            //获取父级菜单下拉数据
            object menuInfoOptions = _menuInfoBll.GetMenuSelectOption(menuInfoId);
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                MenuInfo = menuInfo,
                MenuInfoOptions = menuInfoOptions
            };
            return result;
        }

        /// <summary>
        /// 更新修改页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateMenuInfo(UpdateMenuInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            if (_menuInfoBll.UpdateMenuInfo(model.ID, model.Title, model.Description, model.Level, model.Sort, model.Href, model.ParentId, model.Icon, model.Target))
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                res.Status = 1;
                return result;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="menuInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeleteMenuInfo(string menuInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            UserInfo userInfo = this.GetSessionUserInfo();
            JsonResult result;
            result = new JsonResult(res);
            if (userInfo == null)
            {
                return result;
            }
            else
            {
                if (!userInfo.IsAdmin)
                {
                    res.Msg = "当前用户没有权限";
                    return result;
                }
            }
            if (_menuInfoBll.SoftDeleteMenuInfo(menuInfoId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return result;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 获取下拉数据集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            //获取父级菜单下拉数据
            object menuInfoOptions = _menuInfoBll.GetMenuSelectOption();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                MenuInfoOptions = menuInfoOptions
            };
            return result;
        }
    }
}
